package com.scau.ums.controller;

import com.scau.ums.mapper.ShowAllLostInfoMapper;
import com.scau.ums.model.Lostandfoundinfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//显示失物招领的列表
@Controller
public class showLostList {
    final static int pageSize = 6;

    @Autowired
    public ShowAllLostInfoMapper mapper;//显示全部失物数据

    @RequestMapping("/showlist")
    @ResponseBody
    public Map<String, Object> showlist(HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        //请求的页码数据
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        //请求的关键字
        String keyword = (String)request.getParameter("keyword");
        if(keyword == null)
            keyword = "";

        int sumLostInfo = mapper.getCheckedLostInfoSumByKeyword(keyword);//所有记录数目
        int start = pageSize * (curPage - 1);//当前页的起始记录下标
        int sumPage = (int) Math.ceil((double)sumLostInfo / pageSize);//页码总数

        //查询数据库所有失物信息
        List<Lostandfoundinfo> lostList = mapper.getCheckedLostInfoPageByKeyword(start,pageSize,keyword);
        map.put("lostInfoList", lostList);
        map.put("curPage", curPage);
        map.put("keyword", keyword);
        map.put("sumPage", sumPage);
        map.put("totalRecord", sumLostInfo);
        return map;//返回json，由lostlist.jsp处理
    }

    @RequestMapping("/getlist")
    public String getlist(){
        return "lostlist";
    }

    @RequestMapping("/jytest")
    public String test(){
        return "jy_test";
    }
}
